/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package es.srn.utils.plantillas.PanelDados;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import es.srn.utils.plantillas.PanelDados.MyDicePanel.dadoListener;

/**
 *
 * @author Alumno
 */
public class pruebaDados extends java.awt.Frame {

    List<MyDicePanel> lstDado = new ArrayList<>();
    Map<MyDicePanel, Boolean> dirDado = new HashMap<>();
    
    /**
     * Creates new form pruebaDados
     */
    public pruebaDados() {
        initComponents();
        //configuramos la pantalla
        this.setPreferredSize(new Dimension(500,500));
        this.setMinimumSize(new Dimension(500,500));
        
        //SpinnerNumberModel model =; //initial - min - max - step
        txtTamanoDado.setModel(new SpinnerNumberModel(20,20,99,10)); 
        txtTamanoDado.addChangeListener(new ChangeListener() {
                @Override
                public void stateChanged(ChangeEvent e) {                            
                    jSpinner_stateChanged(e);
                }		
        });
        
        //SpinnerNumberModel model =; //initial - min - max - step
        txtNumeroDados.setModel(new SpinnerNumberModel(1,1,99,1)); 
        txtNumeroDados.addChangeListener(new ChangeListener() {
                @Override
                public void stateChanged(ChangeEvent e) {                            
                    jSpinner_stateChanged(e);
                }		
        });
        
        btnAccion.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(btnAccion.isSelected()){
                    tirar();
                    btnAccion.setText("Parar");
                }else{
                    btnAccion.setText("Tirar");
                    parar();
                }
            }
        });
        
        cargarPanelDados();
    }
    
    private void blockControls(Boolean setBlock)
    {
        txtNumeroDados.setEnabled(setBlock ? false : true);
        txtTamanoDado.setEnabled(setBlock ? false : true);
        
    }
    
    private void jSpinner_stateChanged(ChangeEvent e){
        //System.out.println("Evento del spinner");
        cargarPanelDados();
    }
    
    private void cargarPanelDados(){
        
        //System.out.println("Carga el panel: dados-> " +(Integer)txtNumeroDados.getValue() + " tamaño:-> " +  (int)txtTamanoDado.getValue());
        
        MyDicePanel dado = null;
        Dimension tam = new Dimension((int)txtTamanoDado.getValue(), (int)txtTamanoDado.getValue());
        
        //vaciamos el listado y el panel para volverlo a llenar
        lstDado.removeAll(lstDado);
        dirDado.clear();
        pnlTablero.removeAll();
        pnlTablero.validate();
        
        for (int i = 0; i< (Integer)txtNumeroDados.getValue(); i++)
        {
            dado = new MyDicePanel(1);
            //dado.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));        
            dado.setPreferredSize(tam);
            dado.setName(String.valueOf(i));
            lstDado.add(dado);
            dirDado.put(dado, Boolean.FALSE);
            pnlTablero.add(dado);
            dado.addListadoListener(new dadoListener() {
                @Override
                public void onDadoStop(MyDicePanel dado, int resultado) {
                    //System.out.println("Ha finalizado " + dado.getName() + " con el resultado: " + resultado);
                    dadoFinalizado(dado, resultado);
                }
            });
            
        }
          
        pnlTablero.validate();
    }
    
    private void tirar()
    {
        lblRes.setText("");
        
        for(MyDicePanel dado : lstDado){
            dirDado.put(dado, Boolean.TRUE);
            dado.start(150,10000,0);
        }
    }
    
    private void parar()
    {
        for(MyDicePanel dado : lstDado){
            dado.stop();
        }
    }
    
    private void dadoFinalizado(MyDicePanel dado, int resultado)
    {    
        System.out.println("Ha parado: " + dado.getName() + " con resultado " + resultado);
        lblRes.setText(lblRes.getText() + resultado + " ");
        dirDado.put(dado, Boolean.FALSE);
        if(!dirDado.containsValue(Boolean.TRUE)){
            System.out.println("Han parado todos");
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jPanel5 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtNumeroDados = new javax.swing.JSpinner();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        txtTamanoDado = new javax.swing.JSpinner();
        jPanel2 = new javax.swing.JPanel();
        btnAccion = new javax.swing.JToggleButton();
        pnlTablero = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        lblRes = new javax.swing.JLabel();

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });
        setLayout(new java.awt.GridBagLayout());

        jPanel5.setBackground(new java.awt.Color(255, 0, 0));
        jPanel5.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jLabel1.setText("Numero Dados");
        jPanel5.add(jLabel1);

        txtNumeroDados.setOpaque(false);
        txtNumeroDados.setPreferredSize(new java.awt.Dimension(40, 18));
        jPanel5.add(txtNumeroDados);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        add(jPanel5, gridBagConstraints);

        jPanel1.setBackground(new java.awt.Color(255, 153, 0));
        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jLabel2.setText("Tamaño dados");
        jPanel1.add(jLabel2);

        txtTamanoDado.setPreferredSize(new java.awt.Dimension(60, 18));
        jPanel1.add(txtTamanoDado);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        add(jPanel1, gridBagConstraints);

        jPanel2.setBackground(new java.awt.Color(0, 0, 0));
        jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        btnAccion.setText("Tirar");
        jPanel2.add(btnAccion);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        add(jPanel2, gridBagConstraints);

        pnlTablero.setBackground(new java.awt.Color(0, 0, 204));
        pnlTablero.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        add(pnlTablero, gridBagConstraints);

        jPanel3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel3.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        jLabel3.setText("Resultados:");
        jPanel3.add(jLabel3);
        jPanel3.add(lblRes);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 1;
        gridBagConstraints.weightx = 1.0;
        add(jPanel3, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Exit the Application
     */
    private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
        System.exit(0);
    }//GEN-LAST:event_exitForm

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new pruebaDados().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JToggleButton btnAccion;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JLabel lblRes;
    private javax.swing.JPanel pnlTablero;
    private javax.swing.JSpinner txtNumeroDados;
    private javax.swing.JSpinner txtTamanoDado;
    // End of variables declaration//GEN-END:variables
}
